import { request } from "../../request/index.js";
import { showModal, showToast, uploadFile, chooseImage, showLoading } from "../../util/AsyncWx.js";
let a = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: '',
    MyGoods: [],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (a) {
      a = false;
      return
    } else {
      let pages = getCurrentPages();
      let currentPage = pages[pages.length - 1];
      let options = currentPage.options;
      const { id } = options;
      this.setData({
        uid: id
      })
      this.getMyGoods(id);
    }
  },

  handleImagePreview: function (e) {
    a = true
    var that = this
    var url = e.currentTarget.dataset.url
    wx.previewImage({
      current: url,
      urls: [url],
    });

  },
  async getMyGoods(id) {
    var that = this
    var id = that.data.uid
    const result = await request({ url: "/goods/getGoodsByUid", data: { id } });
    that.setData({
      MyGoods: result
    })

  },
  async delectGoods(e) {
    var that = this
    var id = e.currentTarget.dataset.id
    const result = await showModal({ content: "确认删除吗？" });
    if (result.confirm) {
      const result = await request({ url: "/goods/deleteGoodsById", data: { id } });
      var id = that.data.uid
      that.getMyGoods(id)
      showToast({ title: "删除成功" });

    }
  },
  updateGoods(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/goods_update/goods_update?id=' + id
    });


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})